package com.ww.springboot.boot.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ww.springboot.boot.enumeration.NumEnum;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ww.springboot.boot.model.UrlManage;
import com.ww.springboot.boot.service.UrlManageService;
import com.ww.springboot.boot.utils.JacksonUtil;

/**
 * @author wanwei
 * @TODO 页面导航管理
 * @date: 2018年11月30日 上午9:40:15
 */
@Controller
@RequestMapping("/urlEdit")
public class UrlEditController {

    @Autowired
    private UrlManageService urlManageService;

    @RequestMapping("")
    public String init() {

        return "url/listEdit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String dataList() {
        List<UrlManage> urls = urlManageService.findAll();
        for (UrlManage urlManage : urls) {
            urlManage.setName(urlManage.getName() + getPinyin(urlManage.getName()));
        }
        return JacksonUtil.toJson(urls);
    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 添加子节点
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/right")
    public String right(String id, ModelMap map) {
        map.addAttribute("pId", id);
        return "url/addSon";

    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 点击次数
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/click")
    @ResponseBody
    public void click(String id, ModelMap map) {
        //编辑页面不记录点击次数
        urlManageService.updateOperateTime(Long.valueOf(id));
    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 添加一级目录
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/addFirst")
    @ResponseBody
    public ModelAndView addFirst() {
        UrlManage urlManage = new UrlManage();
        urlManage.setName("一级目录");
        urlManage.setpId(0L);
        urlManageService.insert(urlManage);
        ModelAndView view = new ModelAndView("redirect:/urlEdit");
        return view;
    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 更新
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/updatePre")
    public String updatePre(String id, ModelMap map) {
        UrlManage urlManage = urlManageService.findById(Long.valueOf(id));
        map.addAttribute("urlManage", urlManage);
        return "url/update";
    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 修改
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/update")
    public ModelAndView update(UrlManage urlManage, ModelMap map) {
        urlManageService.update(urlManage);
        ModelAndView view = new ModelAndView("redirect:/urlEdit");
        return view;
    }

    /**
     * @param id
     * @return
     * @author wanwei
     * @TODO 清缓存
     * @date: 2018年11月30日 下午4:15:27
     */
    @RequestMapping("/clearCache")
    public ModelAndView clearCache() {
        urlManageService.clearCache();
        ModelAndView view = new ModelAndView("redirect:/urlEdit");
        return view;
    }

    @RequestMapping("/addSon")
    public ModelAndView addSon(UrlManage urlManage) {
        urlManageService.insert(urlManage);
        ModelAndView view = new ModelAndView("redirect:/urlEdit");
        return view;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        urlManageService.delete(Long.valueOf(id));
        return "删除成功";
    }

    public static String getPinyin(String china) {
        HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
        formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        formart.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] arrays = china.trim().toCharArray();
        String result = "";
        try {
            for (int i = 0; i < arrays.length; i++) {
                char ti = arrays[i];
                if (Character.toString(ti).matches("[\\u4e00-\\u9fa5]")) { //匹配是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti, formart);
                    result += temp[0].charAt(0);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ObjectUtil.isEmpty(result) ? "" : "-" + result;
    }
}
